﻿using IMark.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMark.UC
{
    public partial class TopHeaderCtrl : System.Web.UI.UserControl
    {
        //public string UserName {
        //    get
        //    {
        //        if (Utils.ReadCookie("UserName") != null)
        //        {
        //            return Utils.ReadCookie("UserName");
        //        }
        //        return "";
        //    }
        //}

        //public string PassWord {
        //    get
        //    {
        //        if (Utils.ReadCookie("PassWord") != null)
        //        {
        //            return Utils.ReadCookie("PassWord");
        //        }
        //        return "";
        //    }
        //}
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["User"] != null)
                {
                    KHACH_HANG kh = Session["User"] as KHACH_HANG;
                    lblTen.Text = kh.HoTen;
                    List<SAN_PHAM> ltSP = SanPhamModels.LaySanPhamYeuThich(kh.ID);
                    if (ltSP != null)
                        lblYeUThich.Text = ltSP.Count().ToString();
                    else
                        lblYeUThich.Text = "0";
                    MultiView1.ActiveViewIndex = 1;
                }
                else
                {
                    lblYeUThich.Text = "0";
                }
            }

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string url = HttpContext.Current.Request.Url.AbsoluteUri;
            string username = txtUsername.Text;
            string password = txtPassword.Text;

            KHACH_HANG KhachHang = KhachHangModels.DangNhap(username.Trim(), Utils.ConvertMD5(password.Trim()));
            if (KhachHang == null)
            {
                loginbox.Style.Add("display", "block");
                lblError.Text = "Tên đăng nhập hoặc mật khẩu không đúng.";
            }
            else
            {
                Session["User"] = KhachHang;
                Response.Redirect(url);
            }

        }

        protected void lbtDangXuat_Click(object sender, EventArgs e)
        {
            string url = HttpContext.Current.Request.Url.AbsoluteUri;
            Session.Remove("User");
            Response.Redirect(url);
        }
    }
}